package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsUserMerchantDiscountAddRequest;
import com.sgzn.sgpay.model.request.SgsUserMerchantDiscountListRequest;
import com.sgzn.sgpay.model.request.SgsUserMerchantDiscountRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.UserMerchantDiscountVo;
import com.sgzn.sgpay.persistence.dao.TUserMerchantDiscountMapper;
import com.sgzn.sgpay.persistence.domain.TUserMerchantDiscount;
import com.sgzn.sgpay.persistence.domain.TUserMerchantDiscountExample;
import com.sgzn.sgpay.persistence.manager.MerchantDaoManager;
import com.sgzn.sgpay.service.SgsUserMerchantDiscountService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by linliangfang 13580393408
 * DATETIME:2018/6/3 16:58
 * Description:用户优惠账户接口
 * @author linliangfang 13580393408
 */
@Service
@Slf4j
public class SgsUserMerchantDiscountServiceImpl implements SgsUserMerchantDiscountService {

    @Autowired
    private TUserMerchantDiscountMapper tUserMerchantDiscountMapper;

    @Autowired
    private MerchantDaoManager merchantDaoManager;


    /**
     * 初始化用户商户优惠记录
     */
    @Override
    public  SgsResultResponse<String> iniUserMerchantDiscount(SgsUserMerchantDiscountAddRequest request){
        try{
            TUserMerchantDiscount tUserMerchantDiscount = new TUserMerchantDiscount();
            tUserMerchantDiscount.setUserId(request.getUserId());
            tUserMerchantDiscount.setMerchantId(request.getMerchantId() );
            tUserMerchantDiscount.setDiscountAccountAmount(Integer.valueOf(request.getDiscountAmount()));
            tUserMerchantDiscount.setHadDiscountLevel(request.getHadDiscountLevel());
            tUserMerchantDiscount.setCreatedAt(request.getReqTime());
            tUserMerchantDiscount.setUpdatedAt(request.getReqTime());
            int id = tUserMerchantDiscountMapper.insertSelective(tUserMerchantDiscount);
            if(id > 0){
                return SgsResponseUtil.INSTANCE.success("success");
            }else{
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_DISCOUNT_ADD_ERR.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_DISCOUNT_ADD_ERR.getResponseDesc());
            }
        }catch (Exception e){
            log.error("初始化用户商户优惠记录：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }


    /**
     * 累加用户商户优惠积分
     */
    @Override
    public  SgsResultResponse<String> increaseUserMerchantDiscount(SgsUserMerchantDiscountRequest request){
        try{
            SgsResultResponse<TUserMerchantDiscount> sgsResultResponse = queryUserMerchantDiscount( request );
            if( sgsResultResponse == null ){
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseDesc());
            }
            TUserMerchantDiscount   tUserMerchantDiscountOrigin = sgsResultResponse.getResult();


            //where条件
            TUserMerchantDiscountExample tUserMerchantDiscountExample = new TUserMerchantDiscountExample();
            TUserMerchantDiscountExample.Criteria criteria = tUserMerchantDiscountExample.createCriteria();
            criteria.andUserIdEqualTo(tUserMerchantDiscountOrigin.getUserId());
            criteria.andMerchantIdEqualTo(tUserMerchantDiscountOrigin.getMerchantId());

            //Update Set Value
            TUserMerchantDiscount tUserMerchantDiscountTarget = new TUserMerchantDiscount();
            //累加
            tUserMerchantDiscountTarget.setDiscountAccountAmount( Integer.parseInt( request.getDiscountAmount() ) + tUserMerchantDiscountOrigin.getDiscountAccountAmount());
            tUserMerchantDiscountTarget.setHadDiscountLevel(request.getHadDiscountLevel());
            tUserMerchantDiscountTarget.setCreatedAt(new Date());
            int id = tUserMerchantDiscountMapper.updateByExample(tUserMerchantDiscountTarget,tUserMerchantDiscountExample );
            if(id > 0){
                return SgsResponseUtil.INSTANCE.success("success");
            }else{
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_DISCOUNT_UPDATE_ERR.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_DISCOUNT_UPDATE_ERR.getResponseDesc());
            }
        }catch (Exception e){
            log.error("累加用户商户优惠积分：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }



    /**
     * 消费(减)用户商户优惠积分
     */
    @Override
    public  SgsResultResponse<String> decreaseUserMerchantDiscount(SgsUserMerchantDiscountRequest request){
        try{
            SgsResultResponse<TUserMerchantDiscount> sgsResultResponse = queryUserMerchantDiscount( request );
            if( sgsResultResponse == null ){
                log.info("没有对应的用户商户子账户记录");
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseDesc());
            }

            //条件:
            TUserMerchantDiscount   tUserMerchantDiscountOrigin = sgsResultResponse.getResult();
            TUserMerchantDiscountExample tUserMerchantDiscountExample = new TUserMerchantDiscountExample();
            TUserMerchantDiscountExample.Criteria criteria = tUserMerchantDiscountExample.createCriteria();
            criteria.andUserIdEqualTo(tUserMerchantDiscountOrigin.getUserId());
            criteria.andMerchantIdEqualTo(tUserMerchantDiscountOrigin.getMerchantId());

            //累减 Update Set
            TUserMerchantDiscount tUserMerchantDiscountTarget = new TUserMerchantDiscount();
            if( Integer.parseInt( request.getDiscountAmount() ) > tUserMerchantDiscountOrigin.getDiscountAccountAmount() ){
                log.info(" 用户商户优惠积分不足,请使用用户积分消费 ");
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MCHANT_DISCOUNT_NOT_ENOUGH.getResponseCode(),SgsResponseCode.C_USER_MCHANT_DISCOUNT_NOT_ENOUGH.getResponseDesc());
            }
            tUserMerchantDiscountTarget.setDiscountAccountAmount( tUserMerchantDiscountOrigin.getDiscountAccountAmount() - Integer.parseInt( request.getDiscountAmount() ) );
            tUserMerchantDiscountTarget.setCreatedAt(new Date());

            int id = tUserMerchantDiscountMapper.updateByExample(tUserMerchantDiscountTarget,tUserMerchantDiscountExample );
            if(id > 0){
                return SgsResponseUtil.INSTANCE.success("success");
            }else{
                return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_DISCOUNT_UPDATE_ERR.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_DISCOUNT_UPDATE_ERR.getResponseDesc());
            }
        }catch (Exception e){
            log.error("消费(减)用户商户优惠积分：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

    /**
     * 查询消费用户商户优惠积分(userId,MerchantId)
     */
    @Override
    public SgsResultResponse<TUserMerchantDiscount> queryUserMerchantDiscount(SgsUserMerchantDiscountRequest request){
        SgsResultResponse<TUserMerchantDiscount> resultResponse = new SgsResultResponse<>();

        TUserMerchantDiscountExample tUserMerchantDiscountExample = new TUserMerchantDiscountExample();
        TUserMerchantDiscountExample.Criteria criteria = tUserMerchantDiscountExample.createCriteria();
        criteria.andUserIdEqualTo(request.getUserId());
        criteria.andMerchantIdEqualTo(request.getMerchantId());
        List<TUserMerchantDiscount> tUserMerchantDiscountList =  tUserMerchantDiscountMapper.selectByExample(tUserMerchantDiscountExample);
        if (tUserMerchantDiscountExample == null ){
            log.info("没有对应的用户商户子账户记录");
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseDesc());
        }
        if (tUserMerchantDiscountList.size() == 0 ){
            log.info("没有对应的用户商户子账户记录");
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseDesc());
        }
        TUserMerchantDiscount tUserMerchantDiscountOrigin  = tUserMerchantDiscountList.get(0);
        resultResponse.setResult(tUserMerchantDiscountOrigin);
        resultResponse.setErrorCode(SgsResponseCode.C200.getResponseCode());
        resultResponse.setErrorMsg(SgsResponseCode.C200.getResponseDesc());
        return resultResponse;
    }



    /**
     * 查询用户商户优惠列表
     */
    @Override
    public SgsResultResponse<List<UserMerchantDiscountVo>> queryList(SgsUserMerchantDiscountListRequest request){
        SgsResultResponse<List<UserMerchantDiscountVo>> resultResponse = new SgsResultResponse<>();
        TUserMerchantDiscountExample tUserMerchantDiscountExample = new TUserMerchantDiscountExample();
        TUserMerchantDiscountExample.Criteria criteria =  tUserMerchantDiscountExample.createCriteria();
        criteria.andUserIdEqualTo(request.getUserId());

        List<TUserMerchantDiscount> tUserMerchantDiscountList = tUserMerchantDiscountMapper.selectByExample(tUserMerchantDiscountExample);
        if( tUserMerchantDiscountList == null ){
            log.info("没有找到对应的优惠记录");
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseCode(),SgsResponseCode.C_USER_MERCHANT_NOT_FOUND.getResponseDesc());
        }
        //对象转换
        List<UserMerchantDiscountVo> userMerchantDiscountVoList = new ArrayList<>();
        for (TUserMerchantDiscount tUserMerchantDiscount :tUserMerchantDiscountList ){
            UserMerchantDiscountVo userMerchantDiscountVo = new UserMerchantDiscountVo();
            userMerchantDiscountVo.setUserId( tUserMerchantDiscount.getUserId() );
            userMerchantDiscountVo.setMerchantName(merchantDaoManager.getMerchantByMerchId(tUserMerchantDiscount.getMerchantId()).getMerchantName() );
            userMerchantDiscountVo.setDiscountAmount(tUserMerchantDiscount.getDiscountAccountAmount().toString());
            userMerchantDiscountVoList.add(userMerchantDiscountVo);
        }

        resultResponse.setResult(userMerchantDiscountVoList);
        resultResponse.setErrorCode(SgsResponseCode.C200.getResponseCode());
        resultResponse.setErrorMsg(SgsResponseCode.C200.getResponseDesc());
        return resultResponse;
    }


}
